package com.wangwen.wechatmp.utils;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.io.xml.XmlFriendlyNameCoder;

import java.io.InputStream;
import java.util.*;

/**
 * @Author: wangwei
 * @Date: 2019/11/30 10:43
 */

public class XmlUtils {
    /**
     *
     * @param inputXml
     * @param type
     * @return
     * @throws Exception
     */
    public static Object xml2Object(String inputXml, Class<?> type) throws Exception {
        if (null == inputXml || "".equals(inputXml)) {
            return null;
        }
        XStream xstream = new XStream(new DomDriver("UTF-8", new XmlFriendlyNameCoder("-_", "_")));
        xstream.alias("xml", type);
        return xstream.fromXML(inputXml);
    }

    /**
     *
     * @param inputStream
     * @param type
     * @return
     * @throws Exception
     */
    public static Object xml2Object(InputStream inputStream, Class<?> type) throws Exception {
        if (null == inputStream) {
            return null;
        }
        XStream xstream = new XStream(new DomDriver("UTF-8", new XmlFriendlyNameCoder("-_", "_")));
        xstream.alias("xml", type);
        return xstream.fromXML(inputStream, type);
    }

    /**
     *
     * @param ro
     * @param types
     * @return
     * @throws Exception
     */
    public static String object2Xml(Object ro, Class<?> types) throws Exception {
        if (null == ro) {
            return null;
        }
        XStream xstream = new XStream(new DomDriver("UTF-8", new XmlFriendlyNameCoder("-_", "_")));
        xstream.alias("xml", types);
        return xstream.toXML(ro);
    }

    /**
     *
     定义签名，微信根据参数字段的ASCII码值进行排序 加密签名,故使用SortMap进行参数排序
     */
    public static String createSign(String characterEncoding, SortedMap<String,Object> parameters,String key){
        StringBuffer sb = new StringBuffer();
        Set es = parameters.entrySet();
        Iterator it = es.iterator();
        while(it.hasNext()) {
            Map.Entry entry = (Map.Entry)it.next();
            String k = (String)entry.getKey();
            Object v = entry.getValue();
            if(null != v && !"".equals(v)
                    && !"sign".equals(k) && !"key".equals(k)) {
                sb.append(k + "=" + v + "&");
            }
        }
        //最后加密时添加商户密钥，由于key值放在最后，所以不用添加到SortMap里面去，单独处理，编码方式采用UTF-8
        sb.append("key=" + "3202007009200246561ms05848941904");
        System.out.println("-----------参与签名的字符串为参与签名的字符串为----------"+sb.toString());
        String sign = MD5Util.MD5Encode(sb.toString(), characterEncoding).toUpperCase();
        return sign;
    }
    /**
     *
     定义签名，微信根据参数字段的ASCII码值进行排序 加密签名,故使用SortMap进行参数排序
     */
    public static String createSignNoKey(String characterEncoding, SortedMap<String,Object> parameters){
        StringBuffer sb = new StringBuffer();
        Set es = parameters.entrySet();
        Iterator it = es.iterator();
        while(it.hasNext()) {
            Map.Entry entry = (Map.Entry)it.next();
            String k = (String)entry.getKey();
            Object v = entry.getValue();
            if(null != v && !"".equals(v)
                    && !"sign".equals(k) && !"key".equals(k)) {
                sb.append(k + "=" + v + "&");
            }
        }
        String str = sb.toString();
        str = str.substring(0,str.length() -1);
        //最后加密时添加商户密钥，由于key值放在最后，所以不用添加到SortMap里面去，单独处理，编码方式采用UTF-8
        System.out.println("-----------参与签名的字符串为参与签名的字符串为----------"+str);
        String sign = MD5Util.MD5Encode(str, characterEncoding).toUpperCase();
        return sign;
    }
    public static void main(String[] args) {
//        String appid = "wxd930ea5d5a258f4f";
//        String mch_id = "10000100";
//        String device_info = "1000";
//        String body = "test";
//        String nonce_str = "ibuaiVcKdpRxkhJA";

        SortedMap<String,Object> parameters = new TreeMap<String, Object>();
        parameters.put("orderId", "68");
//        parameters.put("page", "1");
////        parameters.put("lastPay", 1);
//        parameters.put("deviceNo", "NccLFYg7aVXHOTEL11");

        String characterEncoding = "UTF-8";
//        String mySign = createSign(characterEncoding,parameters,"");
        String mySign = createSignNoKey(characterEncoding,parameters);
        System.out.println("微信的签名是：" + mySign);


    }
}
